<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\workerman\controller\WorkerEvents;
use GatewayWorker\Lib\Gateway;
use think\Config;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功','');
    }


    //隐私协议
    public function privacy()
    {
        $array = Config::get('site.privacy');
        $this->success('', $array);
    }


    //用户协议
    public function user()
    {
        $array = Config::get('site.user');
        $this->success('', $array);
    }

    public function intro()
    {
        $data=Db::name('role')->where('status','1')
            ->select();
        foreach($data as $k=>$v){
            $v['image']=cdnurl($v['image'],true);
            $new[$v['type']][]=$v;
        }
        $this->success('',$new);
    }

    public function my()
    {
        $data=Db::name('user')->where('id',$this->auth->id)->field('id,nickname,group_id,avatar,duration,score')->find();
        $data['avatar']=cdnurl($data['avatar'],true);
        $all=Db::name('user')
            ->field('id,nickname,avatar,updatetime,score')
            ->order(['score'=>'desc','updatetime'=>'desc'])
            ->select();
        $rank=0;
        foreach($all as $k=>$v){
            $rank=$rank+1;
            if($v['id']==$this->auth->id){
                $data['rank']=$rank;
                break;
            }
        }
        $this->success('',$data);
    }

    public function gameLog()
    {
        $param=$this->request->param();
        $data=Db::name('game_log')->alias('g')
            ->join('user u','g.user_id=u.id','left')
            ->join('shop s','g.shop_id=s.id','left')
            ->field('g.*,s.name as shop_name,DATE_FORMAT(FROM_UNIXTIME(g.createtime), "%Y年%m月%d日 %H:%i:%s") AS createtime')
            ->paginate($param['size']);
        $this->success('',$data);
    }

    public function directLog()
    {
        $param=$this->request->param();
        $data=Db::name('direct_log')->alias('d')
            ->join('user u','d.user_id=u.id','left')
            ->join('shop s','d.shop_id=s.id','left')
            ->where('d.user_id',$this->auth->id)
            ->field('d.*,s.name as shop_name,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y年%m月%d日 %H:%i:%s") AS createtime')
            ->paginate($param['size']);
        foreach($data as $k=>$v){
            $win=explode(',',$v['win_user_id']);
            $avatar=Db::name('user')->where('id','in',$win)->column('avatar');
            $new=[];
            foreach($avatar as $key=>$value){
                $value=cdnurl($value,true);
                $new[$key]=$value;
            }
            $v['avatar']=$new;
            $data[$k]=$v;
        }
        $this->success('',$data);
    }

    public function rank()
    {
        $param=$this->request->param();
        $data=Db::name('user')
            ->field('id,nickname,avatar,updatetime,score')
            ->order(['score'=>'desc','updatetime'=>'desc'])
            ->paginate($param['size']);
        foreach($data as $k=>$v){
            $v['avatar']=cdnurl( $v['avatar'],true);
            $data[$k]=$v;
        }
        $this->success('',$data);
    }


}
